from django.shortcuts import render
from rest_framework.filters import OrderingFilter
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from drf_haystack.viewsets import HaystackViewSet
from goods.serializers import SKUSerializers, SKUIndexSerializer


# Create your views here.


# GET /categories/(?P<category_id>\d+)/skus/
# 获取分类商品数据

class SKUListView(ListAPIView):
    serializer_class = SKUSerializers

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id)

    # def get(self, request):
    #     '''
    #     获取分类商品的数据
    #     1. 获取分类商品的列表数据
    #     2. 序列化返回
    #     '''
    #     # 1. 获取分类商品的列表数据
    #     category_id = self.category_id
    #     skus = SKU.objects.filter(category_id=category_id)
    #     # 2. 序列化返回
    #     serializer = self.get_serializer(skus, many=True)
    #     serializer = self.get_queryset()
    #     return Response(serializer.datae)

    # 排序

    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time', 'price', 'sales')



# 搜索
class SkUSearchViewSet(HaystackViewSet):
    '''搜索'''
    # 指定索引模型类
    index_models = [SKU]
    # 指定搜索结果子啊系列化是所使用的序列化器类
    # 在haystock搜索出的结果对象中包含两个属性：
    # text, object 搜索字段 , 搜索出的模型对象
    serializer_class = SKUIndexSerializer




